<?php

function list_jurnal_gaji(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');
	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Uraian'=>array('style'=>'text-align:left;border-bottom:2px solid;width:60%'),  
		'Kode '=>array('style'=>'text-align:left;border-bottom:2px solid;width:25%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
	$com = isset($_GET['com']) ? $_GET['com']: '';
	$task = isset($_GET['task']) ? $_GET['task']: '';
	$field = isset($_GET['field']) ? $_GET['field']: '';
	$key = isset($_GET['key']) ? $_GET['key']: '';
	
	$facebox=true;
	$query = "SELECT * FROM jurnal_gaji ORDER BY jurnal_gaji_id ASC ";
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =20;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$com}&task={$task}&field={$field}&key={$key}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	$pager_url_last = null;
	$kgPagerOBJ = new kgPager();
			$kgPagerOBJ->pager_set(
				$pager_url, 
				$total_records, 
				$scroll_page, 
				$per_page, 
				$current_page, 
				$inactive_page_tag, 
				$previous_page_text, 
				$next_page_text, 
				$first_page_text, 
				$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page);  
	
	$i = ($current_page  - 1 ) * 20; 
	$row = array();

	while($ey = my_fetch_array($result) ){
		$i++;
		if($facebox){
			$editproperty = array(
				'href'=>'index.php?com=jurnal_gaji&task=edit&id='.$ey['jurnal_gaji_id'] ,
				'rel'=>'facebox',
				'title'=>'Edit'
			);	
		}else{
			$editproperty = array(
				'href'=>'index.php?com=jurnal_gaji&task=edit&id='.$ey['jurnal_gaji_id'] ,
				'title'=>'Edit'
			);
		}
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
 
		$query="SELECT komponen_gaji_id FROM pr_komponen_gaji 
			WHERE jurnal_gaji_id=".$ey['jurnal_gaji_id']; 
		$rtx = my_query($query);
		if (my_num_rows( $rtx) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['jurnal_gaji_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}		

		$row[] = array(
			'#'=>position_text_align ($i, 'center'),    
			'nama'=>  $ey['nama_jurnal_gaji'] ,     
			'kode'=>  $ey['kode_jurnal_gaji'] ,     
			'operasi'=> position_text_align( $edit_button .' '.$delete_button , 'right'),
		);
	}
	$datas = table_rows($row);
	$paging = $kgPagerOBJ ->showPaging();
	return table_builder($header , $datas ,  5 , false,$paging   );
}

function form_jurnal_gaji_create($jurnal_gaji_id){
	$view = form_header( "Jurnal Gaji" , "Jurnal Gaji"  );
	$fields = my_get_data_by_id('jurnal_gaji','jurnal_gaji_id',$jurnal_gaji_id);
	
	$nama = array(
		'name'=>'nama_jurnal_gaji',
		'id'=>'nama_jurnal_gaji',
		'value'=>isset($_POST['nama_jurnal_gaji']) ? $_POST['nama_jurnal_gaji'] : $fields['nama_jurnal_gaji'],
		'type'=>'text',
		'size'=>'45'
	);
	$form_nama_jurnal_gaji = form_dynamic($nama);
	$view .= form_field_display( $form_nama_jurnal_gaji , "Uraian" );	

	$kode = array(
		'name'=>'kode_jurnal_gaji',
		'type'=>'textfield',
		'id'=>'kode_jurnal_gaji',
		'value'=>isset($_POST['kode_jurnal_gaji']) ? $_POST['kode_jurnal_gaji'] : $fields['kode_jurnal_gaji'],
	);
	$form_kode_jurnal_gaji = form_dynamic($kode);
	$view .= form_field_display( $form_kode_jurnal_gaji , "Kode" );
	
	$opsi = array('D'=>'Debet','K'=>'Kredit');
	$dbk = array(
		'name'=>'dbk',
		'id'=>'dbk',
		'value'=>isset($_POST['dbk']) ? $_POST['dbk'] : $fields['dbk'],
	);
	$form_dbk = form_radiobutton($dbk,$opsi);
	$view .= form_field_display( $form_dbk , "Debet/Kredit" );
	$submit = array(
		'value' => ( $jurnal_gaji_id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function form_jurnal_gaji_submit($jurnal_gaji_id = 0){
	$datas = array(
			'kode_jurnal_gaji'=>my_type_data_str($_POST['kode_jurnal_gaji']), 		 	 	 	 	 	 	 
			'nama_jurnal_gaji'	=>my_type_data_str($_POST['nama_jurnal_gaji']), 
		);
	if($jurnal_gaji_id > 0){
		return my_update_record('jurnal_gaji','jurnal_gaji_id',$jurnal_gaji_id, $datas);
	}
	return my_insert_record('jurnal_gaji' , $datas);
}

function form_jurnal_gaji_validate($jurnal_gaji_id){
	$errsubmit = false;
	$err = array();
	 
	$code =  trim($_POST["kode_jurnal_gaji"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Kode belum di isi";
	}
 
	$name = trim($_POST["nama_jurnal_gaji"]);
	if($name == ''){
		$errsubmit = true;
		$err[] = "Uraian belum diisi";
	}


	$query="SELECT jurnal_gaji_id FROM jurnal_gaji 
		WHERE nama_jurnal_gaji='".$name.
		"' AND jurnal_gaji_id<>".$jurnal_gaji_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Uraian sudah digunakan";	
	}
  
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_jurnal_gaji($jurnal_gaji_id){
	$query = "DELETE FROM jurnal_gaji WHERE jurnal_gaji_id ={$jurnal_gaji_id} ";
	return my_query($query);
}	
